import { getDraftList, createColumn, getContent, setContent } from './service';
import { getDomainCookie } from '@/utils/utils';
import config from '@/../config';
import { message } from 'antd';

export default {
  namespace: 'draftModel',
  state: {
    //可收藏多种网课列表
    data: {},
    types: [], //网课类型列表
    csrf: '', //登录token
    aid: '', //草稿的aid
  },
  reducers: {
    //统一保存数据用法
    save(state: any, action: { payload: any }) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
  effects: {
    //获取我的学习列表的专栏id
    *getId({ payload }: unknown, { call, put }: unknown) {
      let id: number = 0;
      const draftList: unknown = yield call(getDraftList, payload);
      const data: any = draftList;
      if (data.artlist) {
        const { drafts } = data.artlist || [];
        for (let i = 0; i < drafts.length; i++) {
          if (drafts[i].title === config.myOnlineClassName) {
            id = drafts[i].id;
          }
        }
      }
      //若没有这个专栏 创建
      if (id === 0) {
        const { csrf } = payload;
        const newId_promise: Promise<any> = yield put({
          type: 'createColumn',
          payload: { csrf },
        });
        id = yield newId_promise;
      }
      return id;
    },
    //获取我的学习列表的内容
    *getContent({ payload }: unknown, { call, put }: unknown) {
      const bili_jct: unknown = yield getDomainCookie(
        'http://www.bilibili.com',
        'bili_jct',
      ) || ''; //取cookie
      const csrf: string = (<any>bili_jct).value; //csrf
      //拿到我的学习列表aid
      const getId_promise: Promise<any> = yield put({
        type: 'getId',
        payload: { csrf },
      });
      const aid: number = yield getId_promise; //aid
      yield put({
        type: 'save',
        payload: { csrf, aid },
      });
      const content: unknown = yield call(getContent, { aid }); //拿到我的学习列表 内容
      const data: any = content;
      if (data.data) {
        let { content = '' } = data.data;
        content = content
          .replace('<p>', '')
          .replace('</p>', '')
          .replace(/&#34;/g, '"');
        if (content === '') {
          console.log('内部没有数据');
          return;
        }
        let obj = JSON.parse(content) || [];
        // console.log('列表', obj);
        yield put({
          type: 'save',
          payload: {
            data: obj, //网课数据
            types: Object.keys(obj), //网课类型列表
          },
        });
      } else {
        console.log('读取服务器,失败');
      }
    },
    //创建专栏为我的学习列表
    *createColumn({ payload }: unknown, { call }: unknown) {
      const column: unknown = yield call(createColumn, payload);
      const data: any = column;
      if (data.code === 0) {
        const { aid = '' } = data.data;
        return aid;
      } else {
        return 0;
      }
    },
    //删除收藏中的指定视频
    *deleteVideo({ payload }: unknown, { select, put }: unknown) {
      const { bvid, type } = payload;
      const { data } = yield select((state: any) => state.draftModel);
      const _videos = data[type].filter((video: any) => video.bvid !== bvid);
      const _data = { ...data };
      _data[type] = _videos;
      yield put({
        type: 'save',
        payload: { data: _data },
      });
    },
    //保存修改后的data
    *saveData({ _ }: unknown, { select, call, put }: unknown) {
      const { data, csrf, aid } = yield select(
        (state: any) => state.draftModel,
      );
      // console.log(data, csrf, aid)
      const res: unknown = yield call(setContent, { data, csrf, aid });
      const _res: any = res;
      if (_res.code === 0) {
        message.success('更新成功');
        yield put({
          type: 'save',
          payload: { isCollection: true },
        });
      }
    },
  },
};
